package is.client.controller.command;

import is.client.Locale;
import is.client.Main;
import is.common.interfaces.Command;

import java.io.IOException;
import java.net.ConnectException;
import java.net.UnknownHostException;

/**
 * @author aspr0
 *         Date: 26.04.12
 *         Time: 5:51
 */
public class ConnectCommand implements Command
{
    private String server;
    private int port;
    private int controlPort;

    public ConnectCommand(String server, String port, String controlPort)
    {
        this.server = server;
        try
        {
            this.port = Integer.parseInt(port);
            this.controlPort = Integer.parseInt(controlPort);
        }
        catch (NumberFormatException e)
        {
            Main.getController().getUserInterface().exposeErrMsg(Locale.ERR_WRONG_PORT_FORMAT);
        }
    }

    @Override
    public void execute()
    {
        try
        {
            Main.getController().getModel().activate(server, port, controlPort);
            Main.getController().getUserInterface().exposeConnectionEstablished();
        }
        catch (ConnectException e)
        {
            Main.getController().getUserInterface().exposeErrMsg(Locale.ERR_CONNECT);
//            e.printStackTrace();
        }
        catch (UnknownHostException e)
        {
            Main.getController().getUserInterface().exposeErrMsg(Locale.ERR_UNKNOWN_HOST);
//            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

}
